/**
 * File: NodeMapper.java
 * Created by: mhaimel
 * Created on: Mar 8, 2010
 * CVS:  $Id: NodeMapper.java 1.0 Mar 8, 2010 3:58:04 PM mhaimel Exp $
 */
package uk.ac.ebi.curtain.model.graph.io;

import uk.ac.ebi.curtain.model.graph.Graph;
import uk.ac.ebi.curtain.model.graph.Node;

/**
 * @author mhaimel
 *
 */
public class NodeMapper<N,A> implements WriteMapper<Node<N,A>>, ReadMapper<Node<N,A>> {

	private static final String[] header = new String[]{"NodeId","status","active"};

	@Override
	public String[] getHeader() {
		return header;
	}

	@Override
	public Object[] toArray(Node<N,A> node) {
		return new Object[]{
				node.getId(),
				node.getStatus().get(),
				boolean2Char(node.getActive().get()),
		};
	}

	@Override
	public int getFieldCount() {
		return header.length;
	}
	
	protected Character boolean2Char(boolean val){
		if(val){
			return 'T';
		} else {
			return 'F';
		}
	}

	protected boolean char2Boolean(char c){
		switch (c) {
		case 'T':
			return true;
		case 'F':
			return false;
		default:
			throw new IllegalArgumentException("Character not Valid: " + c);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Node<N, A> loadValue(String[] array, int i, Graph graph) {
		Long id = Long.valueOf(array[i++]);
		Integer status = Integer.valueOf(array[i++]);
		Boolean active = char2Boolean(array[i++].charAt(0));
		Node node = graph.getEmptyNode(id);
		node.setStatus(status);
		node.setActive(active);
		return node;
	}
	
}
